package com.ugoodtech.mdcc.web.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.AutoBrand;
import com.ugoodtech.mdcc.core.domain.QStationLeanPaintReport;
import com.ugoodtech.mdcc.core.domain.StationCategory;
import com.ugoodtech.mdcc.core.domain.StationLeanPaintReport;
import com.ugoodtech.mdcc.core.repository.StationLeanPaintReportRepository;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import com.ugoodtech.mdcc.web.service.StationLeanPaintReportService;
import com.ugoodtech.mdcc.web.service.UserProfileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class StationLeanPaintReportServiceImpl implements StationLeanPaintReportService {

    private static final String TYPE_COMPLETE = "complete";

    private static final String TYPE_UNFINSSHED = "unfinished";

    @Autowired
    private StationLeanPaintReportRepository stationLeanPaintReportRepository;

    @Autowired
    private UserProfileService userProfileService;

    @Autowired
    private UserRepository userRepository;


    @Override
    public Page<StationLeanPaintReport> queryReport(String province, String city, StationCategory category, AutoBrand brand, String param, String userParam, String stationNum, Pageable pageable) {
        QStationLeanPaintReport qStationLeanPaintReport = QStationLeanPaintReport.stationLeanPaintReport;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotEmpty(stationNum)) {
            builder.and(qStationLeanPaintReport.station.stationNum.like("%" + stationNum + "%"));
        }
        if (!StringUtils.isEmpty(province)) {
            builder.and(qStationLeanPaintReport.station.province.eq(province));
        }
        if (!StringUtils.isEmpty(city)) {
            builder.and(qStationLeanPaintReport.station.city.eq(city));
        }
        if (null != category) {
            builder.and(qStationLeanPaintReport.station.category.eq(category.name()));
        }
        if (null != brand) {
            builder.and(qStationLeanPaintReport.station.brand.eq(brand.name()));
        }
        if (!StringUtils.isEmpty(param)) {
            builder.and(qStationLeanPaintReport.station.name.like("%" + param + "%"));
        }
        if (!StringUtils.isEmpty(userParam)) {
            builder.and(qStationLeanPaintReport.user.id.in(userProfileService.queryLikeUserProfile(userParam)));
        }
//        if (!StringUtils.isEmpty(code)) {
//            builder.and(qStationLeanPaintReport.serialNumber.like("%"+code+"%"));
//        }
//        if (!StringUtils.isEmpty(name)) {
//            builder.and(qStationLeanPaintReport.realName.like("%"+name+"%"));
//        }
//        if (!StringUtils.isEmpty(telephone)) {
//            builder.and(qStationLeanPaintReport.telephone.like("%"+telephone+"%"));
//        }
//        if (null!=departmentIds&&departmentIds.length>0) {
//            builder.and(qStationLeanPaintReport.departments.any().id.in(departmentIds));
//        }
        Page<StationLeanPaintReport> stationLeanPaintReports = stationLeanPaintReportRepository.findAll(builder, new PageRequest(0, 1));
        return stationLeanPaintReports;
    }
}
